#ifndef __FILE_API_H__
#define __FILE_API_H__

#include "gbl_types.h"

/* ### Local Declarations
 * ################################# */

/* invalid filename characters */
#define FILE_SCWILDCARD '?'     /* Wildcard for single character */
#define FILE_MCWILDCARD '*'     /* Wildcard for multiple characters */
#define FILE_SPECIALAT '@'      /* Wildcard for multiple characters */
#define FILE_CSEPARATOR '\\'    /* Separator characters */
#define FILE_ALT_CSEPARATOR '/' /* Alt Separator characters */
#define FILE_EOS '\0'           /* Terminating character marks end of string */

#ifndef SEEK_SET
#define SEEK_SET (0)
#endif

#ifndef SEEK_CUR
#define SEEK_CUR (1)
#endif

#ifndef SEEK_END
#define SEEK_END (2)
#endif

#define FILE_NAME_SIZE 128 /* Maximum length of filename       */

/* ################################# */
typedef char FDI_TCHAR;
typedef WORD FILE_ID; /* identifier for the file stream */

typedef struct tag_file_info
{
  /* filename plus end of string character */
  FDI_TCHAR file_name[FILE_NAME_SIZE + 1];
  int time;   /* updated time stamp when modified */
  int date;   /* updated date stamp when modified */
  DWORD size; /* size of file data in bytes */
  WORD owner_id;
  WORD group_id;
  WORD permissions;
  FILE_ID data_id; /* FDI identifier for file data */

  /* the following fields are needed for power loss recovery */

  FILE_ID plr_id; /* used for power loss recovery */
  int plr_time;   /* used for power loss recovery */
  int plr_date;   /* used for power loss recovery */
  DWORD plr_size; /* used for power loss recovery */
} FILE_INFO;

typedef enum fatTYPE_eAttributes
{
  fatTYPE_NORMAL = 0,     /* Writable file */
  fatTYPE_RDONLY = 0x01,  /* Read Only file */
  fatTYPE_HIDDEN = 0x02,  /* Hidden from directory view */
  fatTYPE_SYSTEM = 0x04,  /* Owned by the operating system */
  fatTYPE_VOLUME = 0x08,  /* Volume label entry */
  fatTYPE_DIR = 0x10,     /* Sub-directory */
  fatTYPE_ARCHIVE = 0x20, /* Copy of the file exists on this or another medium */
  fatTYPE_LONG_NAME = fatTYPE_RDONLY | fatTYPE_HIDDEN | fatTYPE_SYSTEM | fatTYPE_VOLUME
} fatTYPE_eAttributes;

typedef struct
{
  fatTYPE_eAttributes st_mode; /* File or directory attributes */
  DWORD st_size;               /* File size in bytes */
  unsigned int st_time;        /* Timestamp of last modification */
} fatTYPE_stat;

/*
 * USAGE:
 *    file_identifier = FDI_fopen(filename_ptr, wb);
 */
FILE_ID FDI_fopen(const FDI_TCHAR *, const char *);

/*
 * USAGE:
 *    actual_written = FDI_fwrite(&new_struct, sizeof(NEW_STRUCT), 1, file_id);
 */
size_t FDI_fwrite(const void *, size_t, size_t, FILE_ID);

/* USAGE:
 *    actual_read = FDI_fread(&new_struct, sizeof(NEW_STRUCT), 1, stream_id);
 */
size_t FDI_fread(void *, size_t, size_t, FILE_ID);

/*
 * USAGE:
 *    close_status = FDI_fclose(stream_identifier);
 */
int FDI_fclose(FILE_ID);

/*
 * USAGE:
 *    status = FDI_fseek(stream_identifier, offset, wherefrom);
 */
int FDI_fseek(FILE_ID, long, int);

/*
 * USAGE:
 *    status = FDI_feof(stream);
 */
int FDI_feof(FILE_ID);

/*
 * USAGE:
 *    status = FDI_findfirst(filename_ptr, &the_file_info);
 */
int FDI_findfirst(const FDI_TCHAR *, FILE_INFO *);

/*
 * USAGE:
 *    status = FDI_findnext(&the_file_info);
 */
int FDI_findnext(FILE_INFO *);

/*
 * USAGE:
 *    status = FDI_remove(filename_ptr);
 */
int FDI_remove(const FDI_TCHAR *);

/*
 * USAGE:
 *    status = FDI_rename(oldfilename_ptr, newfilename_ptr);
 */
int FDI_rename(const FDI_TCHAR *, const FDI_TCHAR *);

/*
 * function: FDI_Stat - Get file status
 *
 */
int FDI_Stat(const char *file_name, fatTYPE_stat *stat);

/*
 * function: FDI_Access - Check file is exist
 *
 */
int FDI_Access(const char *path, UINT32 mode);

/*
 * function: MkDir - Create DIR
 *
 * syntax: int FDI_MkDir(const char *);
 */
int FDI_MkDir(const char *);

/*
 * function: GetFreeSpaceSize - Get Free Space Size
 *
 * syntax: unsigned int FDI_GetFreeSpaceSize(void);
 */
unsigned int FDI_GetFreeSpaceSize(void);

/*
 * function: FDI_GetUsedSpaceSize - Get Used Space Size
 *
 * syntax: unsigned int FDI_GetUsedSpaceSize(void);
 */
unsigned int FDI_GetUsedSpaceSize(void);

/*
 * function: FDI_GetTotalSpaceSize - Get Total Space Size
 *
 * syntax: unsigned int FDI_GetTotalSpaceSize(void);
 */
unsigned int FDI_GetTotalSpaceSize(void);

/*========================================================================
 *  PSM API
 *========================================================================*/
/*
 * function: psm_set_wrapper - Set PSM item value
 *
 */
int psm_set_wrapper(char *mod_name, char *subspace, char *variable, char *value);

/*
 * function: psm_get_wrapper - Get PSM item value
 *
 */
char *psm_get_wrapper(char *mod_name, char *subspace, char *variable);

/*
 * function: psm_commit__ - write into flash
 *
 */
int psm_commit__(void);

#endif /* __FILE_API_H__ */
